package com.zxq.ruoyi.network.tcp.handler.impl;


import com.zxq.factory.FastFrame;
import com.zxq.ruoyi.network.tcp.handler.dispatch.TCPDispatchHandler;
import com.zxq.ruoyi.network.tcp.handler.register.SFRegisterFrame;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.util.HexBin;

@Slf4j
public class SFRegistrationHandler extends TCPDispatchHandler {

    final byte head_flag = (byte) 0xff;




    @Override
    public boolean isThisFrame() {
        byte b = in.readByte();
        if(b == head_flag) {
            return true;
        }
        return false;
    }



    @Override
    protected String inflateDevId(byte[] frame) {
        SFRegisterFrame sfRegisterFrame = new SFRegisterFrame();
        FastFrame.decoder(sfRegisterFrame,frame);
        log.info(String.format("inflate devId:%s",sfRegisterFrame.getDevId()));
        return sfRegisterFrame.getDevId();
    }

    @Override
    protected Boolean isRegisterFrame() {

        return true;
    }
   //FF0000000001FF
    @Override
    protected byte[] trimFrame() {
        byte[] frame = new byte[7];
        do {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }while (in.readableBytes() < frame.length);
        in.readBytes(frame);
        return frame;
    }
}
